<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Cp extends CI_Controller {

    var $data = array();
    var $meta = array();
    var $role;
    var $user_id;

    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('session');
        $this->meta['site_title'] = 'Control Panel | GO-BLOG';
        $this->data['open'] = $this->load->view('template/open', $this->meta, TRUE);
        $this->data['close'] = $this->load->view('template/close', '', TRUE);
        $this->role = $this->session->userdata('role');
        $this->user_id = $this->session->userdata('user_id');
        $this->data['user_role'] = $this->role;
    }

    public function index() {
        $this->_authenticate();
        $this->add_article();
    }

    private function _error() {
        show_404();
    }

    //AUTHENTICATION//

    private function _authenticate() {
        if ($this->session->userdata('islogged') == null) {
            redirect(base_url() . 'cp/login');
        }
    }

    private function _is_admin() {
        if ($this->role != 0) {
            $this->_error();
        }
    }

    private function _authorize($user_id) {
        if (($this->user_id != $user_id) && ($this->role != 0)){
            $this->_error();
        }
    }

    //ARTICLE//

    public function list_article() {
        $this->_authenticate();
        $this->load->model('articles');
        $this->data['menu'] = $this->load->view('cp/menu', '', TRUE);
        if ($this->role == 0) {
            $this->meta['article'] = $this->articles->get_all(TRUE);
        } else {
            $this->meta['article'] = $this->articles->get_article('author_id', $this->user_id,TRUE);
        }
        $this->data['content'] = $this->load->view('article/list_article', $this->meta, TRUE);
        $this->data['user_name'] = $this->session->userdata('name');
        $this->load->view('cp/front', $this->data);
    }

    public function add_article() {
        $this->_authenticate();
        $this->load->model('categories');
        $this->data['menu'] = $this->load->view('cp/menu', '', TRUE);
        $this->meta['category'] = $this->categories->get_all(false);
        $this->data['content'] = $this->load->view('article/add_article', $this->meta, TRUE);
        $this->data['user_name'] = $this->session->userdata('name');
        $this->load->view('cp/front', $this->data);
    }

    public function edit_article($article_id) {
        $this->_authenticate();
        $this->load->model('articles');
        $this->load->model('categories');
        $this->session->set_userdata('edit_article_id', $article_id);
        $this->data['menu'] = $this->load->view('cp/menu', '', TRUE);
        $result = $this->articles->get_article_by_id($article_id);
        if (($result->author_id != $this->user_id) && ($this->role != 0)) {
            $this->_error();
        } else {
            $this->meta['category'] = $this->categories->get_all(false);
            $this->meta['article'] = $result;
            $this->meta['image'] = $this->articles->get_image('article_id',$article_id);
            $this->data['content'] = $this->load->view('article/edit_article', $this->meta, TRUE);
            $this->data['user_name'] = $this->session->userdata('name');
            $this->load->view('cp/front', $this->data);
        }
    }
    
    //IMAGE//
    public function edit_image($image_id) {
        $this->_authenticate();
        $this->load->model('articles');
        $this->session->set_userdata('edit_image_id', $image_id);
        $this->data['menu'] = $this->load->view('cp/menu', '', TRUE);
        $result = $this->articles->get_article_by_image_id($image_id);
        if (($result->author_id != $this->user_id) && ($this->role != 0)) {
            $this->_error();
        } else {
            $this->meta['image'] = $this->articles->get_image_by_id($image_id);
            $this->data['content'] = $this->load->view('article/edit_image', $this->meta, TRUE);
            $this->data['user_name'] = $this->session->userdata('name');
            $this->load->view('cp/front', $this->data);
        }
    }
    

    //CATEGORY//
    
    public function list_category() {
        $this->_authenticate();
        $this->load->model('categories');
        $this->data['menu'] = $this->load->view('cp/menu', '', TRUE);
        $this->meta['category'] = $this->categories->get_all(TRUE);
        $this->data['content'] = $this->load->view('category/list_category', $this->meta, TRUE);
        $this->data['user_name'] = $this->session->userdata('name');
        $this->load->view('cp/front', $this->data);
    }
    
    public function add_category() {
        $this->_authenticate();
        $this->data['menu'] = $this->load->view('cp/menu', '', TRUE);
        $this->data['content'] = $this->load->view('category/add_category', $this->meta, TRUE);
        $this->data['user_name'] = $this->session->userdata('name');
        $this->load->view('cp/front', $this->data);
    }
    
    public function edit_category($category_id) {
        $this->_authenticate();
        $this->load->model('categories');
        $this->session->set_userdata('edit_category_id', $category_id);
        $this->data['menu'] = $this->load->view('cp/menu', '', TRUE);
        $result = $this->categories->get_category_by_id($category_id);
            $this->meta['category_name'] = $result->category_name;
            $this->meta['category_published'] = $result->category_published;
            $this->data['content'] = $this->load->view('category/edit_category', $this->meta, TRUE);
            $this->data['user_name'] = $this->session->userdata('name');
            $this->load->view('cp/front', $this->data);
    }
    
    //USER//

    public function list_user() {
        $this->_authenticate();
        $this->load->model('users');
        $this->data['menu'] = $this->load->view('cp/menu', '', TRUE);
        $this->data['user_name'] = $this->session->userdata('name');
        if (!$this->role) {
            $this->meta['user'] = $this->users->get_all();
            $this->data['content'] = $this->load->view('user/list_user', $this->meta, TRUE);
            $this->load->view('cp/front', $this->data);
        } else {
            $this->edit_user($this->user_id);
        }
    }

    public function add_user() {
        $this->_authenticate();
        $this->_is_admin();
        $this->data['menu'] = $this->load->view('cp/menu', '', TRUE);
        $this->data['content'] = $this->load->view('user/add_user', $this->meta, TRUE);
        $this->data['user_name'] = $this->session->userdata('name');
        $this->load->view('cp/front', $this->data);
    }

    public function edit_user($user_id) {
        $this->_authenticate();
        $this->_authorize($user_id);
        $this->load->model('users');
        $this->session->set_userdata('edit_user_id', $user_id);
        $this->data['menu'] = $this->load->view('cp/menu', '', TRUE);
        $result = $this->users->get_user('user_id', $user_id);
        $result = $result->first_row();
        $this->meta['name'] = $result->name;
        $this->meta['username'] = $result->username;
        $this->meta['role'] = $result->role;
        $this->data['content'] = $this->load->view('user/edit_user', $this->meta, TRUE);
        $this->data['user_name'] = $this->session->userdata('name');
        $this->load->view('cp/front', $this->data);
    }

    //LOGIN LOGOUT//

    public function login() {
        if ($this->session->userdata('islogged')) {
            redirect(base_url() . 'cp');
        }
        $this->data['menu'] = '';
        $this->data['content'] = $this->load->view('cp/login', '', TRUE);
        $this->load->view('cp/front', $this->data);
    }

    public function check_login() {
        $this->load->model('users');
        $input['username'] = $this->input->post('fusername');
        $input['password'] = $this->input->post('fpassword');
        if (!$this->users->check_login($input)) {
            $this->session->set_flashdata('login', 'Login Failed');
            redirect(base_url() . 'cp/login');
        } else {
            $this->session->set_userdata($this->users->get_user_by_username($input['username']));
            $this->session->set_userdata('islogged', true);
            redirect(base_url() . 'cp');
        }
    }

    public function logout() {
        $this->session->sess_destroy();
        redirect(base_url() . 'cp');
    }

}

?>